package com.hex.liyue.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.hex.liyue.LiYueApplication;
import com.hex.liyue.commonmodule.utils.T;
import com.hex.liyue.mainmodule.activity.MainActivity;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * Created by yukoyuan on 16/7/18.
 * 这是一个微信分享的回调页面
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(WXEntryActivity.this, "wxf6a32cee6aa658e2", false);
        api.registerApp("wxf6a32cee6aa658e2");
        api.handleIntent(getIntent(), WXEntryActivity.this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                //分享成功
                T.show(WXEntryActivity.this, "分享成功", 0);
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                T.show(WXEntryActivity.this, "分享取消", 0);
                finish();
                //分享取消
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                T.show(WXEntryActivity.this, "分享失败", 0);
                finish();
                //分享拒绝
                break;
        }
    }

    @Override
    public void onReq(BaseReq arg0) {
        // TODO Auto-generated method stub
        try {
            Intent intent = new Intent(LiYueApplication.getInstance(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            LiYueApplication.getInstance().startActivity(intent);
        } catch (Exception e) {
        }
    }
}
